//
//  WaterflowLayout.m
//  04-瀑布流
//
//  Created by J on 15/11/12.
//  Copyright © 2015年 JZJ. All rights reserved.
//

#import "WaterflowLayout.h"


//static const CGFloat ColumnMargin  = 10;//每列间距
//
//static const CGFloat RowMargin  = ColumnMargin ;//每行间距 居然两个相等  直接让他们想等算了


@interface WaterflowLayout ()


@property(nonatomic,strong)NSMutableDictionary * maxYDict;//这个字典用来存储每一列最大的Y值(每一列的高度)

@end

@implementation WaterflowLayout

- (NSMutableDictionary *)maxYDict{
    
    
    if (!_maxYDict) {
        
//        NSLog(@"2");

        self.maxYDict = [NSMutableDictionary dictionary];
        
        for (int i=0; i<self.columsCount; i++) {//columsCount=3
            
            NSString * column = [NSString stringWithFormat:@"%d",i];
            
//            NSLog(@"1");
            self.maxYDict[column]= @0;
            
        }
        
       
    }
    
    return _maxYDict;
    
}

- (void)prepareLayout{//每次刷新都会调用
    
}


- (instancetype)init{
    if (self=[super init]) {
        
        self.ColumnMargin = 10;
        
        self.RowMargin = 10;
        
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
        self.columsCount = 3;
        
        
        
        
        
        
    }
    
    return self;
    
}


- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    
    
    return YES;
}



//每个item的属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
//    @{@"0",@100,@"1",@200};
    
    NSString* minYColumn = @"0";//假设最短的那一列是第0列
    
    
    __block NSString* minColumn = @"0";
    //找出最短的那一列
    
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString*  column,NSNumber* maxY, BOOL * _Nonnull stop) {
        
        if ([maxY floatValue]< [self.maxYDict[minYColumn] floatValue]) {//取出最小的值
            
            minColumn = column;
        }
        
        
        
        
    }];
    
    NSLog(@"%@",minColumn);
    
    //计算尺寸
    CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left -self.sectionInset.right - (self.columsCount -1) * self.ColumnMargin)/self.columsCount;
    
    CGFloat height = 100+ arc4random_uniform(100);

    
    //计算位置
    //列号 ＊ (一个图片宽度+每列间距)
    CGFloat x =self.sectionInset.left + (width+self.ColumnMargin)* [minColumn integerValue];
   //最大的y在间距
    CGFloat y =[self.maxYDict[minColumn] floatValue] + self.RowMargin;
    
    //更新这列最大y值
    
    self.maxYDict[minColumn] = @(y + height);

    
    
    //创建属性
    
    UICollectionViewLayoutAttributes * attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    attrs.frame=CGRectMake(x, y, width, height);
    
    
    return nil;

    
    
}


//所有item的属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    NSMutableArray * array = [NSMutableArray array];
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    for (int i=0; i<count; i++) {
        
        UICollectionViewLayoutAttributes * attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [array addObject:attrs];
        
    }
    
    return array;

    
}



//内容范围
//- (CGSize)collectionViewContentSize{
//    
//    
//    
//}

@end
